package com.sqlist.share;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.sqlist.share.util.Const;

/**
 * Created by SqList on 2018/6/12.
 */

public class StartActivity extends AppCompatActivity {
    private static final String TAG = "Share - StartActivity";

    private void setStatusBar() {
        // 禁止横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        // 去掉Android原生的标题栏
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

        // 将状态栏设成透明
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = this.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }

        // android6.0以后可以对状态栏文字颜色和图标进行修改
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }

    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;

    private static Handler handler;

    private String username;
    private String token;

    public static Handler getHandler() {
        return handler;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setStatusBar();
        setContentView(R.layout.activity_start);
        Log.i(TAG, "onCreate()");

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                Log.i(TAG, "handler");
                if (msg.what == 0x123) {
                    start();
                }
                return false;
            }
        });

        // 启动页停留5秒
        Log.i(TAG, "启动页停顿3秒");
        Message message = new Message();
        message.what = 0x123;
        handler.sendMessageDelayed(message, 3 * 1000);
    }

    public void start() {
        // 获取 usernameAndToken.xml 内容
        Log.i(TAG, "获取 usernameAndToken.xml 内容");
        preferences = getSharedPreferences("usernameAndToken", MODE_PRIVATE);

        username = preferences.getString("username", null);
        token = preferences.getString("token", null);

        if (username == null || token == null) {
            Log.i(TAG, "No Token");

            Intent intent = new Intent(StartActivity.this, LoginActivity.class);
            startActivity(intent);
        } else {
            Log.i(TAG, "Have Token");
            Log.i(TAG, "username: " + username + ", token: " + token);

            Log.i(TAG, "设置Const中的username和token");
            Const.USERNAME = username;
            Const.TOKEN = token;

            Intent intent = new Intent(StartActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }
}
